<template>
  <div class="home">
    <div class="detail_box">
      <!-- 菜单 -->
      <div class="tab_menu">
        <div>
          <span v-for="(item,index) in menus" :class="{'choose':item.id===tabIndex}"
                @click="chooseTab(item.id)">{{ item.name }}订单</span>
        </div>
      </div>
      <!-- 操作 -->
      <div class="operate_box">
        <div class="item_box">
          <div class="operate_item">
            <label>订单号：</label>
            <el-input type="text" placeholder="请输入订单号" v-model="keyword" clearable></el-input>
          </div>
          <div class="operate_item" v-if="!$route.query.userId">
            <label>用户：</label>
            <userSelect @listenToUser="getUserId" :uId="userId"/>
          </div>
          <div class="operate_item" v-if="!$route.query.userId">
            <label>操作人：</label>
            <el-select
              filterable
              v-model="operateId"
              placeholder="请选择操作人">
              <el-option label="全部" :value="''"></el-option>
              <el-option
                v-for="(item,index) in operateOption"
                :key="index"
                :label="item.userName + '(' + (item.name) + ')'"
                :value="item.id">
              </el-option>
            </el-select>
          </div>
          <div class="operate_item">
            <label>时间：</label>
            <el-date-picker
              v-model="proDate"
              type="datetimerange"
              align="right"
              value-format="yyyy-MM-dd HH:mm:ss"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              clearable
              :default-time="['00:00:00', '23:59:59']">
            </el-date-picker>
          </div>
          <div class="operate_item">
            <span class="operate_btn search_btn" @click="searchData">查询</span>
            <span class="operate_btn reset_btn" @click="resetTable">重置</span>
          </div>
        </div>
<!--        <div class="btn_box" v-if="btn&&btn.createOrder">-->
<!--          <span class="operate_btn search_btn" @click="isCreate=true">生成订单</span>-->
<!--        </div>-->
      </div>
      <!-- 表格 -->
      <div class="flex_box">
        <div class="detail_part" :class="{'left_box':isPop}">
          <div class="table_box">
            <el-table
              border
              height="620"
              v-loading="loading"
              :header-cell-style="tableHeader"
              ref="multipleTable"
              :data="tableInfo"
              tooltip-effect="dark"
              highlight-current-row
              style="width: 100%;">
              <el-table-column
                width="160"
                sortable="custom"
                prop="modifyTime"
                label="修改时间">
                <template slot-scope="scope">
                  <p style="text-align: left">{{ formatTime(scope.row.modifyTime) }}</p>
                </template>
              </el-table-column>

              <el-table-column
                prop="name"
                width="130"
                label="用户名">
                <template slot-scope="scope">{{ scope.row.name ? scope.row.name : '——' }}</template>
              </el-table-column>

              <el-table-column
                width="80"
                prop="userId"
                label="用户ID">
              </el-table-column>

              <el-table-column
                width="230"
                prop="orderNo"
                label="订单号">
              </el-table-column>

              <el-table-column
                width="100"
                prop="type"
                label="支付类型">
                <template slot-scope="scope">{{ scope.row.type=== 4 ? '系统赠送':(scope.row.type=== 6 ? '对公转账' : (scope.row.type=== 7 ? '二维码支付':'纸票支付'))}}</template>
              </el-table-column>

              <el-table-column
                width="160"
                prop="money"
                label="交易金额(元)/数量">
                <template slot-scope="scope">
                  <div>
                    <span style="color: #f7ab20" title="交易金额">{{ scope.row.money ? scope.row.money : 0 }}</span> /
                    <span style="color: #6687f9" title="交易数量">{{ scope.row.numbers ? scope.row.numbers : 0 }}</span>
                  </div>
                </template>
              </el-table-column>
              <el-table-column
                width="160"
                prop="pledgeMoney"
                label="抵押总金额(元)/数量">
                <template slot-scope="scope">
                  <div>
                    <span style="color: #f7ab20"
                          title="抵押总金额">{{ scope.row.pledgeMoney ? scope.row.pledgeMoney : 0 }}</span> /
                    <span style="color: #e40000"
                          title="抵押总数量">{{ scope.row.pledgeNumber ? scope.row.pledgeNumber : 0 }}</span>
                  </div>
                </template>
              </el-table-column>

              <el-table-column
                width="110"
                prop="productMoney"
                label="商品金额(元)">
                <template slot-scope="scope">
                  <div>
                    <span style="color: #f7ab20"
                          title="商品金额">{{ scope.row.productMoney ? scope.row.productMoney : 0 }}</span>
                  </div>
                </template>
              </el-table-column>

              <el-table-column
                label="备注">
                <template slot-scope="scope">
                  <div>
                    {{ scope.row.remark ? scope.row.remark : '——' }}
                  </div>
                </template>
              </el-table-column>

              <el-table-column
                width="80"
                label="状态">
                <template slot-scope="scope">
                  <div :style="'color:'+(scope.row.enable===1?'#67c430':(scope.row.enable===0?'orange':'#e40000'))">
                    {{ statusOption[scope.row.enable]['name'] }}
                  </div>
                </template>
              </el-table-column>


              <el-table-column
                label="操作"
                fixed="right"
                v-if="btn&&(btn.detail || btn.codePay)"
                width="180">
                <template slot-scope="scope">
                  <div class="table_btn">
                    <span class="change_info" @click="openPopup(scope.row)" v-if="btn.detail">详情</span>
                    <span class="change_info" v-if="(scope.row.enable == 4 && scope.row.type===7) && btn.codePay" @click="createCode(scope.row,2)">扫码支付</span>
<!--                    <span class="change_info success_btn" @click="openPay(scope.row,2)"-->
<!--                          v-if="btn.pay && (scope.row.enable !== 1 && (scope.row.enable !== 3))">系统支付</span>-->
<!--                    <span class="change_info success_btn" @click="openPay(scope.row,2)"-->
<!--                          v-if="btn.pay && (scope.row.enable !== 1 && (scope.row.enable !== 3))">水票支付</span>-->
<!--                    <span class="change_info success_btn" @click="openPay(scope.row,2)"-->
<!--                          v-if="btn.pay && (scope.row.enable !== 1 && (scope.row.enable !== 3))">对公支付</span>-->
<!--                    <span class="change_info await" @click="sendPopup(scope.row)"-->
<!--                          v-if="btn.send && (scope.row.enable === 1)">附送赠品</span>-->
                  <!--取消-->
                    <span class="change_info delete_btn" @click="deleteItem(scope.row)"
                          v-if="btn.delete">取消</span>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="record_page">
            <span class="page_count">共<i>{{ totals }}</i>条</span>
            <el-pagination
              @size-change="sizeChange"
              @current-change="getTableInfo"
              background
              :current-page="currentPage"
              :page-sizes="[15, 20, 30, 40]"
              :page-size="pageSize"
              layout="prev, pager, next, sizes,jumper"
              :total="totals">
            </el-pagination>
          </div>
        </div>
        <div class="right_box" v-if="isPop">
          <p class="page_title">{{ orderNo }}订单详情 <i class="detail_close" @click="closeDetail"></i> </p>
          <detail :orderId="orderNo" :isUpdate="update"></detail>
          <p class="page_title">{{ orderNo }}订单日志 <i class="detail_close" @click="closeDetail"></i> </p>
          <log :orderId="orderNo" :isUpdate="update"></log>
        </div>
      </div>

    </div>
    <qrcode :qrcode="qrcode" v-if="isPay"  @listenToChild="listenToEdit"></qrcode>
    <createOrder v-if="isCreate" @listenToChild="listenToEdit"></createOrder>
    <sendGift v-if="isSend" :orderNo="oId" :userId="uId" @listenToChild="listenToEdit"></sendGift>
    <orderCancel v-if="isCancel" :orderNo="orderNo" @listenToChild="listenToEdit"></orderCancel>
  </div>
</template>

<script>

import log from '../log/index'
import detail from '../detail/index'
import createOrder from "./createOrder";
import sendGift from "./sendGift";
import commons from "@/utils/common";
import {delData, listData} from "@/api/data/user/order";
import {getBtn} from "../../../utils/btnAuth";
import orderCancel from "./orderRemark";
import userSelect from '../../../components/user/index.vue'
import {getSenderList} from "@/api/data/user/productReturn";
import axios from "axios";
import {restUrl} from "../../../../url";
import {getToken} from "@/utils/auth";
import qrcode from "../../system/user/qrcode";
export default {
  components: {
    log,
    detail,
    createOrder,
    sendGift,
    orderCancel,
    userSelect,
    qrcode
  },
  data() {
    return {
      menus: [{name:'系统赠送',id:4},{name:'对公转账',id:6},{name:'二维码支付',id:7},{name:'纸票支付',id:8}],
      statusOption:[{name:'待支付',color:''},{name:'支付成功',color:''},{name:'支付失败',color:''},{name:'已取消',color:''},{name:'待收款',color:''}],
      mIndex: 4,
      proDate: '',
      keyword: '',
      userId: '',
      currentPage: 1,
      pageSize: 15,
      totals: 0,
      loading: false,
      tableInfo: [],
      isPop: false,
      orderId: '',
      orderNo: '',
      uId: '',
      oId: '',
      isCreate: false,
      isSend: false,
      btn: {},
      update: false,
      isRemark: false,
      remark: '',
      type: '',
      tabIndex: 4,
      operateOption:[],
      operateId:'',
      qrcode:'',
      isPay:false,
      isCancel:false
    }
  },
  mounted() {
    this.btn = getBtn();
    if (this.btn.query) {
      this.getTableInfo(1);
    }
    this.getOperate();
  },
  methods: {
    chooseTab(index) {
      this.isPop = false;
      this.mIndex = index;
      this.tabIndex = index;
      this.getTableInfo(1);
    },
    //获取操作人类型
    getOperate() {
      let that = this;
      getSenderList().then(res => {
        that.operateOption = res;
      }).catch((e) => {
      });
    },
    // 获取列表信息
    getTableInfo(page) {
      let that = this;
      that.loading = true;
      that.currentPage = page;
      let queryParams = {
        limit: that.pageSize,
        page: page,
        orderNo: that.keyword,
        userId: that.$route.query.userId ? parseInt(that.$route.query.userId) : that.userId,
        enable: 4,
        type:that.mIndex,
        startDate: that.proDate ? that.proDate[0] : '',
        endDate: that.proDate ? that.proDate[1] : '',
        operateUserId:that.operateId ? that.operateId : ''
      }
      listData(queryParams).then(res => {
        that.tableInfo = res ? res.list : [];
        that.totals = res ? res.count : 0;
        that.loading = false;
      }).catch((e) => {
        that.loading = false;
      });
    },
    //总金额
    sumMoney(money, pledgeMoney) {
      money ? money = money : money = 0;
      pledgeMoney ? pledgeMoney = pledgeMoney : pledgeMoney = 0;
      return parseFloat(money) + parseFloat(pledgeMoney)
    },

    //关闭详情和日志
    closeDetail() {
      this.isPop = false;
    },

    searchData() {
      this.isPop = false;
      this.getTableInfo(1)
    },

    // 重置
    resetTable() {
      this.pageSize = 15;
      this.keyword = '';
      this.userId = '';
      this.proDate = '';
      this.operateId = '';
      this.searchData()
    },

    getUserId(e) {
      this.userId = e;
    },
    //生成二维码
    createCode(obj){
      let that = this;
      let token = getToken();
      if (token) {
        that.loading = true;
        axios.defaults.headers.common['token'] = token
        axios.get(restUrl + '/data/user/order/pay-page?orderNo=' + obj.orderNo, {responseType: "arraybuffer"}).then(res => {
          if (res.status === 200) {
            return 'data:image/png;base64,' + btoa(new Uint8Array(res.data).reduce((data, byte) => data + String.fromCharCode(byte), ''));
          }
        }).then(function (data) {
          //接收转换后的Base64图片
          that.loading = false;
          // 设置图片地址
          that.qrcode = data;
          that.isPay = true;
        })
      }
    },

    // 删除
    deleteItem(obj) {
      this.orderNo = obj.orderNo;
      this.isCancel = true;
    },

    // 打开弹窗
    openPopup(obj) {
      this.isPop = true;
      this.orderId = obj.id;
      this.orderNo = obj.orderNo;
    },

    sendPopup(obj) {
      this.isSend = true;
      this.oId = obj.orderNo;
      this.uId = obj.userId;
    },

    // 确认
    openPay(obj, type) {
      let that = this;
      that.type = type;
      that.isRemark = true;
      that.orderNo = obj.orderNo;
      that.uId = obj.userId;
    },

    listenToEdit(e) {
      let that = this;
      that.isCreate = false;
      that.isSend = false;
      that.isRemark = false;
      that.isPay = false;
      this.isCancel = false;
      if (e.refresh) {
        that.getTableInfo(that.currentPage)
        that.update = !this.update;
      }
    },

    formatTime(time) {
      return commons.formatTime(time)
    },

    sizeChange(val) {
      this.pageSize = val;
      this.getTableInfo(1)
    },

    // 设置table标题居中
    tableHeader({row, column, rowIndex, columnIndex}) {
      return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600'
    },
  }
}
</script>

<style scoped>
.flex_box {
  display: flex;
}

.detail_part {
  width: 100%;
}

.left_box {
  width: 60%;
}

.right_box {
  width: 40%;
}

.page_title {
  font-weight: 600;
  padding-left: 20px;
  margin-bottom: 10px;
  font-size: 18px;
  position: relative;
}

</style>
